package com.sky.controller.admin;

import com.sky.constant.RedisKeyConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author wjh
 * @create 2023-07-08
 */
@RestController("adminShopController")  //指定生成的Bean叫什么名字，否则会和user包下的ShopController重名
@RequestMapping("/admin/shop")
@Slf4j
public class ShopController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置店铺状态为：{}", status==1 ? "营业中" : "打烊中");
        stringRedisTemplate.opsForValue().set(RedisKeyConstant.SHOP_STATUS,status.toString());
        return Result.success();
    }

    @GetMapping("/status")
    public Result<Integer> getStatus() {
        String shop_status = stringRedisTemplate.opsForValue().get(RedisKeyConstant.SHOP_STATUS);
        if (shop_status != null) {
            return Result.success(Integer.valueOf(shop_status));
        }
        return Result.error("获取店铺营业状态失败");
    }
}
